package org.top10.parser;

import java.util.ArrayList;
import java.util.List;

import org.top10.collector.Collector;
import org.top10.entity.Video;
import org.top10.executor.ParseSource;
import org.top10.util.TOP10Global;
import org.w3c.dom.Node;

public class YoukuTask extends Task{

	private static final int webSiteID = TOP10Global.WEB_SITE_ID_YOUKU;// 对应网站ID
	private static final String ENCoding = "utf-8";// 对应网站ID

	public YoukuTask() {
		super(webSiteID, ENCoding);
	}

	public void loadParser() {
		PARSER.add(new ToukuHotPointParser());
		PARSER.add(new ToukuCartoonParser());
		PARSER.add(new ToukuTVParser());
		PARSER.add(new ToukuMoiveParser());
		PARSER.add(new ToukuRVSHOWParser());
	}

	public static void main(String args[]) throws Exception {
		String url = "http://www.soku.com/search_top/";
	//	ParseSource source = Collector.createCache(url, ENCoding);
		new ToukuRVSHOWParser().dealText(ENCoding);
	}
}

abstract class ToukuParser extends TOPParserImpl {
	private static final String SEARCH_URL="http://www.soku.com";
	public ToukuParser(String c, int t, String u) {
		super(c, t, u);
	}

	public abstract List parse(ParseSource doc) throws Exception;

	public abstract Video build(Node node, int rank) throws Exception;

	protected List parseTouku(Node node)throws Exception{
		
		if(node!=null){
			ArrayList<Node> dls = new ArrayList<Node>();
			getAllTagsByParent(node, "a", dls);
			List<Video> list = new ArrayList<Video>();
			int rank = 1;
			while (rank<11) {
				Video v = build(dls.get(rank-1), rank++);
				System.out.println(v.toString());
				list.add(v);
			}
			return list;
		}
		return null;
	}
	protected  Video buildViedo(Node node, int rank){
		Video v = new Video();
		v.setRank(rank);
		String url = getAttrValueByTag(node, "href");
		v.setPlayUrl(SEARCH_URL+url);
		v.setName(node.getTextContent());
		return v;
	}
}

class ToukuMoiveParser extends ToukuParser {

	public ToukuMoiveParser() {
		super("电影排行", TOP10Global.TYPE_Television,"http://www.soku.com/search_top/");
	}

	private static final String SIGN = "ST_67_0";

	public Video build(Node node, int rank) throws Exception {
		return buildViedo(node,rank);
	}

	public List parse(ParseSource source) throws Exception {
		Node body = source.getDocSource().getElementsByTagName("BODY").item(0);
		Node node = getNodeByAttr("div", "id",SIGN, body);
		return parseTouku(node);
	}
}
class ToukuTVParser extends ToukuParser {

	public ToukuTVParser() {
		super("电视剧排行", TOP10Global.TYPE_Television, "http://www.soku.com/search_top/");
	}

	private static final String SIGN = "ST_72_72";

	public Video build(Node node, int rank) throws Exception {
		return buildViedo(node,rank);
	}

	public List parse(ParseSource source) throws Exception {
		Node body = source.getDocSource().getElementsByTagName("BODY").item(0);
		Node node = getNodeByAttr("div", "id",SIGN, body);
		return parseTouku(node);
	}
}
class ToukuRVSHOWParser extends ToukuParser {

	public ToukuRVSHOWParser() {
		super("娱乐节目", TOP10Global.TYPE_Television, "http://www.soku.com/search_top/");
	}

	private static final String SIGN = "ST_77_77";

	public Video build(Node node, int rank) throws Exception {
		return buildViedo(node,rank);
	}

	public List parse(ParseSource source) throws Exception {
		Node body = source.getDocSource().getElementsByTagName("BODY").item(0);
		Node node = getNodeByAttr("div", "id",SIGN, body);
		return parseTouku(node);
	}
}
class ToukuCartoonParser extends ToukuParser {

	public ToukuCartoonParser() {
		super("动漫排行", TOP10Global.TYPE_Television,"http://www.soku.com/search_top/");
	}

	private static final String SIGN = "ST_80";

	public Video build(Node node, int rank) throws Exception {
		return buildViedo(node,rank);
	}

	public List parse(ParseSource source) throws Exception {
		Node body = source.getDocSource().getElementsByTagName("BODY").item(0);
		Node node = getNodeByAttr("table", "id",SIGN, body);
		return parseTouku(node);
	}
}
class ToukuHotPointParser extends ToukuParser {

	public ToukuHotPointParser() {
		super("热点排行", TOP10Global.TYPE_Television,"http://www.soku.com/search_top/");
	}

	private static final String SIGN = "ST_89";

	public Video build(Node node, int rank) throws Exception {
		return buildViedo(node,rank);
	}

	public List parse(ParseSource source) throws Exception {
		Node body = source.getDocSource().getElementsByTagName("BODY").item(0);
		Node node = getNodeByAttr("table", "id",SIGN, body);
		return parseTouku(node);
	}
}